﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Alos.Common;
using Alos.Entity;
using Alos.IService;
using System.Threading.Tasks;

namespace Alos.LanClass.Api
{
    /// <summary>
    /// 分组信息表
    /// </summary>
    public class GroupInfoController : ApiController
    {
         IGroupInfoService GroupInfoService;
        /// <summary>
        /// 构造函数
        /// </summary>
         /// <param name="_GroupInfoService"></param>
         public GroupInfoController(IGroupInfoService _GroupInfoService)
        {
            GroupInfoService = _GroupInfoService;
        }
         /// <summary>
         /// 获取当前老师和当前班级的分组信息
         /// </summary>
         /// <param name="ClassId">班级ID</param>
         /// <returns>用户指标</returns>
         [HttpGet]
         [Route("api/GroupInfo/getGroupInfo")]
         public async Task<JsonRtn<List<dynamic>>> getGroupInfo(int ClassId)
         {
             var jsRtn = new JsonRtn<List<dynamic>>();
             jsRtn.data = await Task.Factory.StartNew(uid =>
             {
                 return GroupInfoService.GetGroupInfo((int)uid, ClassId).ToList();
             }, Common.Current.User.id);
             return jsRtn;
         }
         /// <summary>
         /// 添加分组
         /// </summary>
         /// <param name="data">分组信息</param>
         /// <returns></returns>
         [HttpPost]
         [Route("api/GroupInfo/CreatGroupInfo")]
         public JsonRtn<dynamic> CreatGroupInfo([FromBody]List<GroupInfo> data)
         {
             var jsRtn = new JsonRtn<dynamic>();
             foreach (GroupInfo GroupInfo in data)
             {
                 jsRtn.message+=GroupInfoService.CreatGroupInfo(GroupInfo)?"添加成功":GroupInfo.GroupName+"添加失败";
             }
             return jsRtn;
         }
         /// <summary>
         /// 修改分组
         /// </summary>
         /// <param name="data">分组信息</param>
         /// <returns></returns>
         [HttpPost]
         [Route("api/GroupInfo/UpdateGroupInfo")]
         public async Task<JsonRtn<List<dynamic>>> UpdateGroupInfo([FromBody]List<GroupInfo> data)
         {
             var jsRtn = new JsonRtn<List<dynamic>>();
             int classid = 0;
             foreach (GroupInfo GroupInfo in data)
             {
                 classid =Convert.ToInt32(GroupInfo.ClassId);
                 if (GroupInfo.ID>0)
                 {
                     jsRtn.message += GroupInfoService.UpdateGroupInfo(GroupInfo) ? "修改成功" : GroupInfo.GroupName + "修改失败";
                 }
                 else
                 {
                     jsRtn.message += GroupInfoService.CreatGroupInfo(GroupInfo) ? "添加成功" : GroupInfo.GroupName + "添加失败";
                 }
                
             }
             jsRtn.data = await Task.Factory.StartNew(uid =>
             {
                 return GroupInfoService.GetGroupInfo((int)uid, classid).ToList();
             }, Common.Current.User.id);
             return jsRtn;
         }
         /// <summary>
         /// 删除分组
         /// </summary>
         /// <param name="id">分组id</param>
         /// <returns></returns>
         [HttpGet]
         [Route("api/GroupInfo/DeleteGroupInfo")]
         public JsonRtn<dynamic> DeleteGroupInfo(int id)
         {
             var jsRtn = new JsonRtn<dynamic>();
             jsRtn.message = GroupInfoService.DeleteGroupInfo(id) ? "删除成功": "删除失败";
             return jsRtn;
         }
    }
}
